#include <jni.h>
#include "com_rfid_jni_JNI_RFIDCaller.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

char* jstring2str(JNIEnv* env, jstring jstr)
{
       char* rtn = NULL;
       jclass clsstring = env->FindClass("java/lang/String");
       jstring strencode = env->NewStringUTF("utf-8");
       jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
       jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
       jsize alen = env->GetArrayLength(barr);
       jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
       if (alen > 0)
       {
                 rtn = (char*)malloc(alen + 1);
                 memcpy(rtn, ba, alen);
                 rtn[alen] = 0;
       }
       env->ReleaseByteArrayElements(barr, ba, 0);
       return rtn;
}

jstring str2jstring(JNIEnv* env, const char* pat)
{
       jclass strClass = env->FindClass("Ljava/lang/String;");
       jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
       jbyteArray bytes = env->NewByteArray(strlen(pat));
       env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
       jstring encoding = env->NewStringUTF("utf-8");
       return (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
}

int readOneArea(int itemNum, unsigned char data[144]) 
{
	int (__stdcall *readOneAreaDataFromRfid)(int,unsigned char[]);
	HINSTANCE hDll;
	hDll=LoadLibrary("HNTRFID.dll");
	readOneAreaDataFromRfid = (int (__stdcall *)(int,unsigned char[]))GetProcAddress(hDll,"readOneAreaDataFromRfid");
	int resultValue = readOneAreaDataFromRfid(itemNum,data);
	FreeLibrary(hDll);
	return resultValue;
}

int readTwoArea(int itemNum, unsigned char data[144]) 
{
	int (__stdcall *readTwoAreaDataFromRfid)(int,unsigned char[]);
	HINSTANCE hDll;
	hDll=LoadLibrary("HNTRFID.dll");
	readTwoAreaDataFromRfid = (int (__stdcall *)(int,unsigned char[]))GetProcAddress(hDll,"readTwoAreaDataFromRfid");
	int resultValue = readTwoAreaDataFromRfid(itemNum,data);
	FreeLibrary(hDll);
	return resultValue;
}

int readThreeArea(int itemNum, unsigned char data[144]) 
{
	int (__stdcall *readThereAreaDataFromRfid)(int,unsigned char[]);
	HINSTANCE hDll;
	hDll=LoadLibrary("HNTRFID.dll");
	readThereAreaDataFromRfid = (int (__stdcall *)(int,unsigned char[]))GetProcAddress(hDll,"readThereAreaDataFromRfid");
	int resultValue = readThereAreaDataFromRfid(itemNum,data);
	FreeLibrary(hDll);
	return resultValue;
}

int writeOneArea(int itemNum, unsigned char data[48])
{
	int (__stdcall *writeOneAreaDataIntoRfid)(int,unsigned char[]);
	HINSTANCE hDll;
	hDll=LoadLibrary("HNTRFID.dll");
	writeOneAreaDataIntoRfid = (int (__stdcall *)(int,unsigned char[]))GetProcAddress(hDll,"writeOneAreaDataIntoRfid");
	int resultValue = writeOneAreaDataIntoRfid(itemNum,data);
	FreeLibrary(hDll);
	return resultValue;
}

int writeTwoArea(int itemNum, unsigned char data[96])
{
	int (__stdcall *writeTwoAreaDataIntoRfid)(int,unsigned char[]);
	HINSTANCE hDll;
	hDll=LoadLibrary("HNTRFID.dll");
	writeTwoAreaDataIntoRfid = (int (__stdcall *)(int,unsigned char[]))GetProcAddress(hDll,"writeTwoAreaDataIntoRfid");
	int resultValue = writeTwoAreaDataIntoRfid(itemNum,data);
	FreeLibrary(hDll);
	return resultValue;
}

int writeThreeArea(int itemNum, unsigned char data[144])
{
	int (__stdcall *writeThereAreaDataIntoRfid)(int,unsigned char[]);
	HINSTANCE hDll;
	hDll=LoadLibrary("HNTRFID.dll");
	writeThereAreaDataIntoRfid = (int (__stdcall *)(int,unsigned char[]))GetProcAddress(hDll,"writeThereAreaDataIntoRfid");
	int resultValue = writeThereAreaDataIntoRfid(itemNum, data);
	FreeLibrary(hDll);
	return resultValue;
}

void throwStatusException(JNIEnv *env, int resultValue) 
{
	char status[5];
	itoa(resultValue, status, 10);
	jclass exceptionClass =env->FindClass("com/rfid/jni/RFIDStatusException");
	env->ThrowNew(exceptionClass, status);
}


JNIEXPORT void JNICALL Java_com_rfid_jni_JNI_1RFIDCaller__1ring
(JNIEnv *env, jobject jobj)
{
	
	char ch[MAX_PATH];
	GetModuleFileName(NULL,ch,MAX_PATH);
	printf("%s\n", ch);
	void (__stdcall *ring)(); 
	HINSTANCE hDll;
	hDll=LoadLibrary("HNTRFID.dll");
	ring = (void (__stdcall *)())GetProcAddress(hDll,"ring");
	ring();
	FreeLibrary(hDll);
}

JNIEXPORT jstring JNICALL Java_com_rfid_jni_JNI_1RFIDCaller__1read
(JNIEnv *env, jobject jobj, jint jareaCount, jint jareaStarts)
{
	unsigned char data[144]={""};
	int resultValue = 0;
	if(1 == (int)jareaCount) 
	{
		resultValue = readOneArea(jareaStarts, data);
	}
	else if(2 == (int)jareaCount)
	{
		resultValue = readTwoArea(jareaStarts, data);
	}
	else if(3 == (int)jareaCount)
	{
		resultValue = readThreeArea(jareaStarts, data);
	}
	else 
	{
		jclass exceptionClass =env->FindClass("com/rfid/RFIDCallException");
		env->ThrowNew(exceptionClass,"argument \"areaCount\" must be between 1 and 3 !");
	}
	if(resultValue != 0)
	{
		throwStatusException(env, resultValue);
	}
	jstring jdata;
	jdata = str2jstring(env, (char*)data);
	return jdata;
}

JNIEXPORT void JNICALL Java_com_rfid_jni_JNI_1RFIDCaller__1write
(JNIEnv *env, jobject jobj, jint jareaCount, jint jareaStarts, jstring jdata)
{
	char* data = jstring2str(env, jdata);
	int resultValue = 0;
	if(1 == (int)jareaCount) 
	{
		resultValue = writeOneArea(jareaStarts, (unsigned char*)data);
	}
	else if(2 == (int)jareaCount)
	{
		resultValue = writeTwoArea(jareaStarts, (unsigned char*)data);
	}
	else if(3 == (int)jareaCount)
	{
		resultValue = writeThreeArea(jareaStarts, (unsigned char*) data);
	}
	else 
	{
		jclass exceptionClass =env->FindClass("com/rfid/RFIDCallException");
		env->ThrowNew(exceptionClass,"argument \"areaCount\" must be between 1 and 3 !");
	}

	if(resultValue != 0)
	{
		throwStatusException(env, resultValue);
	}
}